package com.heima.search.service.Impl;

import com.fasterxml.jackson.databind.util.ArrayBuilders;
import com.heima.es.service.EsService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.search.dto.UserSearchDTO;
import com.heima.search.service.ArticleSearchService;
import com.heima.search.vo.SearchArticleVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.lucene.queryparser.xml.builders.BooleanQueryBuilder;
import org.apache.lucene.search.Query;
import org.elasticsearch.client.license.LicensesStatus;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Stream;

@Service
@Slf4j
public class ArticleSearchServiceImpl implements ArticleSearchService {
    @Value("${file.minio.readPath}")
    String realPath;
    @Autowired
    EsService<SearchArticleVo> esService;
    @Override
    public ResponseResult search(UserSearchDTO dto) {
        //参数检验
    if (dto==null|| StringUtils.isEmpty(dto.getSearchWords())){
        return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
    }
    if (dto.getMinBehotTime()==null){
        dto.setMinBehotTime(new Date());
    }

        //查询条件封装
        SearchSourceBuilder
                sourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        //关键字的查询
        boolQueryBuilder.must(QueryBuilders.matchQuery("title",dto.getSearchWords()));
        //发布时间小于最小时间
        RangeQueryBuilder publishTime = QueryBuilders.rangeQuery("publishTime").lt(dto.getMinBehotTime());
        boolQueryBuilder.filter(publishTime);
        //排序
        sourceBuilder.sort("pulishTime", SortOrder.DESC);

        //分页
        sourceBuilder.from(0);
        sourceBuilder.size(dto.getPageSize());
        //高亮的处理
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.field("title");
        highlightBuilder.preTags("<font style='color: red; font-size: inherit;'>");
        highlightBuilder.postTags("</font>");
        sourceBuilder.highlighter(highlightBuilder);
        sourceBuilder.query(boolQueryBuilder);
        //执行查询
        PageResponseResult result = esService.search(sourceBuilder, SearchArticleVo.class, "app_info_article");

        //执行结果封装
        List<SearchArticleVo> list = (List<SearchArticleVo>) result.getData();
        Stream<SearchArticleVo> searchArticleVoStream = list.stream().map(searchArticleVo -> {
            searchArticleVo.setStaticUrl(realPath + searchArticleVo.getStaticUrl());
            return searchArticleVo;
        });
        return PageResponseResult.okResult(searchArticleVoStream);
    }
}
